package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import services.ShopCarService;
import services.impl.ShopCarServiceImpl;




@WebServlet("/shopcar")
public class ShopCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	ShopCarService shopService = new ShopCarServiceImpl();
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		int user_id = (int) session.getAttribute("uid");//获取用户id
		String godsid = request.getParameter("goods_id");//获取商品id
		System.out.println(user_id+"--"+godsid);
		int res = shopService.insertToCar(user_id,Integer.parseInt(godsid));
		if(res > 0) {
			response.sendRedirect("mycar.jsp");
		}else {
			out.print("购物车已有商品");
			response.sendRedirect("index.jsp");
		}
		
	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
